package ru.st.selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Search extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testSearch() throws Exception {
	  driver.get(baseUrl + "/php4dvd-2.0-patched/php4dvd/");
	    WebElement usernameField = driver.findElement(By.id("username"));
		usernameField.clear();
	    usernameField.sendKeys("admin");
	    WebElement passwordField = driver.findElement(By.name("password"));
		passwordField.clear();
	    passwordField.sendKeys("admin");
	    driver.findElement(By.name("submit")).click();
	    for (int second = 0;; second++) {
	    	if (second >= 60) fail("timeout");
	    	try { if (isElementPresent(By.id("results"))) break; } catch (Exception e) {}
	    	Thread.sleep(1000);
	    } 
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys("My First Film");
    if (isElementPresent(By.xpath("//div[contains(text(), 'My First Film')]"))){
    	System.out.println("The film(s) was(were) found");
    }
    else
    	System.out.println("No films were found");    
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
